package com.anglireports.report.data.campusPage;


import com.alibaba.fastjson.JSONObject;
import com.anglireports.report.bean.photo.TeamPhoto;
import com.anglireports.report.bean.primary.Classroom;
import com.anglireports.report.bean.primary.Course;
import com.anglireports.report.bean.primary.Student;
import com.anglireports.report.bean.primary.Teacher;
import com.anglireports.report.bean.report.Concentration;
import com.anglireports.report.bean.report.Emotion;
import com.anglireports.report.bean.report.Reports;
import com.anglireports.report.bean.report.Vitality;
import com.anglireports.report.data.base.ResponseDataModel;
import com.anglireports.report.data.base.ResultCodeEnum;
import com.anglireports.report.data.chartCommon.*;
import com.anglireports.report.data.common.angli.AngliCommonData;
import com.anglireports.report.data.common.zhujian.CommonData;
import com.anglireports.report.response.anliResult.StudentModel;
import com.anglireports.report.response.result.ReportsResult;
import com.anglireports.report.response.result.TeachersSnaResult;
import com.anglireports.report.response.util.CommonUtils;
import com.anglireports.report.response.util.DateUtil;

import java.util.*;

/**
 * @author lvjilei
 * @ceateTime 2019/3/27.
 */
public class CampusDataUtil {

    public static ResponseDataModel<CampusPageResponse> reponseData(String starttime, String endtime, String campusid){
        ResponseDataModel<CampusPageResponse> responseDataModel = new ResponseDataModel<>();
        if(null == starttime || endtime == null ||
                campusid== null){
            responseDataModel.setErr_code(ResultCodeEnum.PARAM_ERROR.getErr_code());
            responseDataModel.setErr_msg(ResultCodeEnum.PARAM_ERROR.getErr_msg());
            return responseDataModel;
        }

        Map<String, Classroom> classroomMap = CommonData.getClassroomsMap(starttime);
        Classroom classroom = classroomMap.get(campusid);//校区信息

        Map<String,Course > courseMap = CommonData.getCourses(starttime);
        Map<String, Student> studentMap = CommonData.getStudentMap();
        Map<String, Teacher> teacherMap = CommonData.getTeachersMap();
        List<Student> students = new ArrayList<>();//存储该时间段内所有学生
        List<Teacher> teachers = new ArrayList<>();//存储该时间段内所有老师
        List<Course> courses = new ArrayList<>();//存储该时间段内所有老师

        Map<String, Map<String, Map<String, Reports>>> courseReportMap = new HashMap<>();

        //获取时间段内上课记录
        List<Course> coursesList = classroom.getCourses();
        for(Course course : coursesList) {
            ReportsResult reportsResult = CommonData.getReports(starttime,
                    endtime, course.get_id(), null, null);
            if (reportsResult.getResult_code() == 0) {
                Map<String, Map<String, Reports>> reportsMap =
                        megreStudentDataByDate(reportsResult, studentMap, teacherMap, courseMap,
                                students, teachers, courses);
                courseReportMap.put(course.get_id(), reportsMap);
            }
        }
        CampusPageResponse campusPageResponse = new CampusPageResponse();
        //设置基本信息
        setBasicInfo(campusPageResponse, classroom, students, teachers, courses,starttime, endtime);
        //获取出勤信息
        String date = DateUtil.isoStrToTargetStr(starttime, DateUtil.dateFormat2);
        Map<String,Reports> map = courseReportMap.get(courses.get(0).get_id()).get(date);
        if(null != map){
            //获取昂立学生接口数据
            Map<String,StudentModel> angliStuMap = AngliCommonData.getStuMap(courses.get(0).getClass_id());
            setStatisticsInfo1(campusPageResponse, date, map, studentMap, angliStuMap);
        }
        //获取图表信息
        setChartInfo(campusPageResponse, courseReportMap, starttime, endtime, courseMap, courses, studentMap, students);

        //获取精彩镜头
        List<String>  momentPics = new ArrayList<>();
        for (Course course :courses){
            TeachersSnaResult list =
                    CommonData.getTeachersSnapshots(starttime, endtime,course.get_id(), null);
            for (TeamPhoto teamPhoto : list.getTeacherSnapshots()){
                momentPics.add(teamPhoto.getUrl());
            }
        }
        campusPageResponse.setMomentPics(momentPics);
        responseDataModel.setResponse(campusPageResponse);
        return responseDataModel;
    }

    public static void setChartInfo(CampusPageResponse campusPageResponse,
                                    Map<String, Map<String, Map<String, Reports>>> courseReportMap,
                                    String starttime, String endtime, Map<String,Course> courseMap,
                                    List<Course> courses, Map<String, Student> studentMap, List<Student> students){
        List<ChartInfo> danrenpingjun = new ArrayList<>(); //单人所有平均值
        Map<String,Map<String,CourseChartInfo>> allInfo = new HashMap<>();
        Iterator<String> courseSet = courseReportMap.keySet().iterator();
        Iterator<String> dateSet = null;
        Iterator<String> nameSet = null;

        //单人所有指标集合
        Map<String, Concentration> concentrationMap = new HashMap<>();
        Map<String, Emotion> emotionMap = new HashMap<>();
        Map<String, Vitality> vitalityMap = new HashMap<>();

        //所有人指标集合
        Concentration totalConcentration = new Concentration(0, 0, 0, 0);
        Emotion totalEmotion = new Emotion(0, 0 ,0 ,0);
        Vitality totalVitality = new Vitality(0,0,0,0,0);
        Integer yingdao = 0;
        Integer shidao = 0;

        Reports reports = null;

        Concentration courseConcentration = null;
        Emotion courseEmotion = null;
        Vitality courseVitality = null;

        Concentration concentration = null;
        Emotion emotion = null;
        Vitality vitality = null;
        CourseChartInfo courseChartInfo = null;

        //关注度指标最高
        Reports maxConcentration = null;
        //情绪指数最高
        Reports maxEmotion = null;
        //关注度指标最低
        Reports minConcentration = null;
        //情绪指数最低
        Reports minEmotion = null;

        while(courseSet.hasNext()){
            String courseKey = courseSet.next();
            Map<String,Map<String,Reports>> coureReMap = courseReportMap.get(courseKey);


            Set<String> angliSet = AngliCommonData.getStuMap(courseMap.get(courseKey).getClass_id()).keySet();
            dateSet = coureReMap.keySet().iterator();
            int stucount = 0;
            while(dateSet.hasNext()){
                String dateKey = dateSet.next();
                Map<String,Reports> nameReMap = coureReMap.get(dateKey);

                Map<String,CourseChartInfo> courseChartMap = allInfo.get(dateKey);
                if(courseChartMap == null){
                    courseChartMap = new HashMap<>();
                    allInfo.put(dateKey, courseChartMap);
                }

                courseConcentration = new Concentration(0, 0, 0, 0);
                courseEmotion = new Emotion(0, 0 ,0 ,0);
                courseVitality = new Vitality(0,0,0,0,0);

                nameSet = nameReMap.keySet().iterator();
                while (nameSet.hasNext()){
                    String nameKey = nameSet.next();
                    reports = nameReMap.get(nameKey);
                    concentration = reports.getConcentration();
                    emotion = reports.getEmotion();
                    vitality = reports.getVitality();
                    //合并单人所有关注度数据供算单人平均数据
                    if(concentrationMap.get(nameKey) == null){
                        concentrationMap.put(nameKey,concentration);
                    } else {
                        concentrationMap.get(nameKey).megreConcentration(concentration);
                    }
                    //合并单人所有关情绪数据供算单人平均数据
                    if(emotionMap.get(nameKey) == null){
                        emotionMap.put(nameKey,emotion);
                    } else {
                        emotionMap.get(nameKey).megreEmotion(emotion);
                    }
                    //合并单人所有关活跃度数据供算单人平均数据
                    if(vitalityMap.get(nameKey) == null){
                        vitalityMap.put(nameKey,vitality);
                    } else {
                        vitalityMap.get(nameKey).megreVitality(vitality);
                    }

                    totalConcentration.megreConcentration(concentration);
                    totalEmotion.megreEmotion(emotion);
                    totalVitality.megreVitality(vitality);

                    int c = Integer.valueOf(reports.getStudent_count());
                    if(stucount == 0){
                        stucount = c;
                    } else {
                        stucount = stucount > c? stucount:c ;
                    }

                    courseConcentration.megreConcentration(concentration);
                    courseEmotion.megreEmotion(emotion);
                    courseVitality.megreVitality(vitality);

                    //获取最大关注度报告
                    if(maxConcentration == null) {
                        maxConcentration = reports;
                    } else {
                        if (maxConcentration.getConcentration().getConcentrationData()
                                < reports.getConcentration().getConcentrationData()) {
                            maxConcentration = reports;
                        }
                    }

                    //获取最小关注度报告
                    if(minConcentration == null) {
                        minConcentration = reports;
                    } else {
                        if(minConcentration.getConcentration().getConcentrationData()
                                > reports.getConcentration().getConcentrationData()){
                            minConcentration = reports;
                        }
                    }

                    //获取情绪指标最高值
                    if(maxEmotion == null) {
                        maxEmotion = reports;
                    } else {
                        if(maxEmotion.getEmotion().getEmotionData()
                                < reports.getEmotion().getEmotionData()){
                            maxEmotion = reports;
                        }
                    }

                    //获取情绪指标最低值
                    if(minEmotion == null) {
                        minEmotion = reports;
                    } else {
                        if(minEmotion.getEmotion().getEmotionData()
                                > reports.getEmotion().getEmotionData()){
                            minEmotion = reports;
                        }
                    }
                }
                //计算单人单天数据供图表使用
                courseChartInfo = new CourseChartInfo();
                courseChartInfo.setCourseid(courseKey);
                courseChartInfo.setCourseName(courseMap.get(courseKey).getName());
                courseChartInfo.setSdate(dateKey);
                courseChartInfo.setConcentration(courseConcentration.getConcentrationData());
                courseChartInfo.setEmotion(courseEmotion.getEmotionData());
                courseChartInfo.setVitality(courseVitality.getVitalityData());

                courseChartMap.put(courseKey, courseChartInfo);

                yingdao += angliSet.size();
                shidao += stucount;
            }

        }

        //循环计算每个人的平均指标
        Iterator<String> iterator = concentrationMap.keySet().iterator();
        while(iterator.hasNext()){
            String key  = iterator.next();
            Student student = studentMap.get(key);
            ChartInfo chart = new ChartInfo();
            chart.setStudentid(key);
            chart.setStudentName(student.getName_ch());
            chart.setConcentration(concentrationMap.get(key).getConcentrationData());
            chart.setEmotion(emotionMap.get(key).getEmotionData());
            chart.setVitality(vitalityMap.get(key).getVitalityData());
            danrenpingjun.add(chart);
        }

        int n = DateUtil.getDiffDay(starttime, endtime);
        List<String> dates = new ArrayList<>();
        List<String> names = new ArrayList<>();

        for(Course course : courses){
            names.add(course.getName());
        }
        Map<String, DataModel1> scatterDataMap = new HashMap<>();
        Map<String, DataModel1> barData1Map = new HashMap<>();

        String d =
                DateUtil.isoStrToTargetStr(starttime, DateUtil.dateFormat2);
        //整合图表数据
        for( int i = 0 ; i< n ; i++){
            String startdate =
                    DateUtil.formatDate(d, DateUtil.dateFormat2,"MM月dd日");
            dates.add(startdate);
            Map<String, CourseChartInfo> sChartInfo = allInfo.get(d);

            if(sChartInfo == null ){
                d = DateUtil.ndaysDate(d,1,DateUtil.dateFormat2);
                continue;
            }
            for(Course course : courses){
                CourseChartInfo chart = sChartInfo.get(course.get_id());
                if(chart == null ){
                    //scatterData 图
                    DataModel1 model1 = scatterDataMap.get(course.getName());
                    if(model1 == null){
                        List<Float> values = new ArrayList<>();
                        values.add(0f);
                        model1 = new DataModel1(course.getName(), values);
                        scatterDataMap.put(course.getName(), model1);
                    } else {
                        List<Float> values =  model1.getValues();
                        values.add(0f);
                    }
                    //barData1 图
                    DataModel1 model2 = barData1Map.get(course.getName());
                    if(model2 == null){
                        List<Float> values = new ArrayList<>();
                        values.add(0f);
                        model2 = new DataModel1(course.getName(), values);
                        barData1Map.put(course.getName(), model2);
                    }else {
                        List<Float> values =  model2.getValues();
                        values.add(0f);
                    }
                } else {
                    //scatterData 图
                    DataModel1 model1 = scatterDataMap.get(chart.getCourseName());
                    if(model1 == null){
                        List<Float> values = new ArrayList<>();
                        values.add(chart.getConcentration());
                        model1 = new DataModel1(chart.getCourseName(), values);
                        scatterDataMap.put(chart.getCourseName(), model1);
                    } else {
                        List<Float> values =  model1.getValues();
                        values.add(chart.getConcentration());
                    }
                    //barData1 图
                    DataModel1 model2 = barData1Map.get(chart.getCourseName());
                    if(model2 == null){
                        List<Float> values = new ArrayList<>();
                        values.add(chart.getVitality());
                        model2 = new DataModel1(chart.getCourseName(), values);
                        barData1Map.put(chart.getCourseName(), model2);
                    }else {
                        List<Float> values =  model2.getValues();
                        values.add(chart.getEmotion());
                    }
                }
            }
            d = DateUtil.ndaysDate(d, 1,DateUtil.dateFormat2);
        }
        //设置statisticsInfo2
        StatisticsInfo2 statisticsInfo2 = new StatisticsInfo2();
        statisticsInfo2.setPingjunzhi(totalConcentration.getConcentrationData() + "");
        danrenpingjun.sort(new SortComparator(1, "desc"));
        statisticsInfo2.setPingjunzuigao( danrenpingjun.get(0).getConcentration()
                + "(" + danrenpingjun.get(0).getStudentName() + ")") ;
        statisticsInfo2.setPingjunzuidi( danrenpingjun.get(danrenpingjun.size() -1 ).getConcentration()
                + "(" +  danrenpingjun.get(danrenpingjun.size() -1 ).getStudentName() + ")");
        statisticsInfo2.setZuidizhi( minConcentration.getConcentration().getConcentrationData()
                + "(" + studentMap.get(minConcentration.getStudent_id()).getName_ch() +")");
        statisticsInfo2.setZuigaozhi( maxConcentration.getConcentration().getConcentrationData()
                + "(" + studentMap.get(maxConcentration.getStudent_id()).getName_ch() +")");

        campusPageResponse.setStatisticsInfo2(statisticsInfo2);

        ScatterData scatterData = new ScatterData();
        scatterData.setDates(dates);
        scatterData.setData(mapToList(scatterDataMap));
        campusPageResponse.setScatterData(scatterData);

        BarData barData1 = new BarData();
        barData1.setDates(dates);
        barData1.setData(mapToList(barData1Map));
        campusPageResponse.setBarData1(barData1);

        //设置statisticsInfo3
        StatisticsInfo2 statisticsInfo3 = new StatisticsInfo2();
        statisticsInfo3.setPingjunzhi(totalEmotion.getEmotionData() + "");
        danrenpingjun.sort(new SortComparator(2, "desc"));
        statisticsInfo3.setPingjunzuigao( danrenpingjun.get(0).getEmotion()
                + "(" + danrenpingjun.get(0).getStudentName() + ")") ;
        statisticsInfo3.setPingjunzuidi( danrenpingjun.get(danrenpingjun.size() -1 ).getEmotion()
                + "(" +  danrenpingjun.get(danrenpingjun.size() -1 ).getStudentName() + ")");
        statisticsInfo3.setZuidizhi( minEmotion.getEmotion().getEmotionData()
                + "(" + studentMap.get(minEmotion.getStudent_id()).getName_ch() +")");
        statisticsInfo3.setZuigaozhi( maxEmotion.getEmotion().getEmotionData()
                + "(" + studentMap.get(maxEmotion.getStudent_id()).getName_ch() +")");
        campusPageResponse.setStatisticsInfo3(statisticsInfo3);


        List<EmotionData> emotionData = new ArrayList<>();
        emotionData.add(new EmotionData("负面", totalEmotion.getUpsetPercent()));
        emotionData.add(new EmotionData("疑惑", totalEmotion.getQuestionPercent()));
        emotionData.add(new EmotionData("中性", totalEmotion.getNormalPercent()));
        emotionData.add(new EmotionData("正面", totalEmotion.getPassionPercent()));
        campusPageResponse.setEmotionData(emotionData);

        List<Object[]> pieData = new ArrayList<>();
        Object[] chuqinlvArray = {"出勤率", CommonUtils.getNum(((float)shidao/yingdao)*100.0f, "0.0")};
        Object[] zhuanzhuzhishuArray = {"专注指数", totalConcentration.getConcentrationData()};
        Object[] qingxuzhishuArray = {"情绪指数", totalEmotion.getEmotionData()};
        Object[] canyuzhishi = {"参与指数", totalVitality.getVitalityData()};
        pieData.add(chuqinlvArray);
        pieData.add(zhuanzhuzhishuArray);
        pieData.add(qingxuzhishuArray);
        pieData.add(canyuzhishi);
        campusPageResponse.setPieData(pieData);
    }

    /**
     * 同一学生同一天同一课程数据合并
     * @param reportsResult
     * @return
     */
    public static Map<String, Map<String, Reports>>  megreStudentDataByDate(ReportsResult reportsResult,
                                                                            Map<String, Student> studentMap,
                                                                            Map<String, Teacher> teacherMap,
                                                                            Map<String, Course> courseMap,
                                                                            List<Student> students,
                                                                            List<Teacher> teachers,
                                                                            List<Course> courses){
        Map<String, Reports> rMap;
        Reports re;
        Map<String, Map<String, Reports>> reportsMap = new HashMap<String, Map<String, Reports>>();
        List<Reports> reportsList = reportsResult.getReports();

        for (Reports report : reportsList){
            String reportDate = DateUtil.isoStrToTargetStr(
                    report.getStart_time(), DateUtil.dateFormat2);
            String sid = report.getStudent_id();
            String tid = report.getTeacher_id();
            String cid = report.getCourse_id();
            //该段时间该课程所有学生集合
            if (!students.contains(studentMap.get(sid))) {
                students.add(studentMap.get(sid));
            }
            //该段时间该课程所有老师集合
            if (!teachers.contains(teacherMap.get(tid))) {
                teachers.add(teacherMap.get(tid));
            }
            //该段时间该课程所有老师集合
            if (!courses.contains(courseMap.get(cid))) {
                courses.add(courseMap.get(cid));
            }
            rMap = reportsMap.get(reportDate);
            if(null != rMap){
                //同一学生同一天同一课程数据合并
                re = rMap.get(sid);
                if( null != re){
                    rMap.put(sid, re.megreReportsData(report));
                } else {
                    rMap.put(sid, report);
                }
            } else {
                rMap = new HashMap<>();
                rMap.put(sid, report);
                reportsMap.put(reportDate, rMap);
            }
        }
        return reportsMap;
    }


    /**
     * 获取基本信息
     * @param campusPageResponse
     * @param students
     * @param teachers
     * @param courses
     * @param starttime
     * @param endtime
     */
    public static void setBasicInfo(CampusPageResponse campusPageResponse,
                                    Classroom classroom,
                                    List<Student>  students, List<Teacher> teachers,
                                    List<Course> courses, String starttime, String endtime){
        List<String> studentNameList = new ArrayList<>(students.size());
        List<String> teacherNameList = new ArrayList<>(teachers.size());
        List<String> coureNameList = new ArrayList<>(courses.size());

        for (Student student : students){
            studentNameList.add(student.getName_ch());
        }
        for (Teacher teacher : teachers){
            teacherNameList.add(teacher.getName_ch());
        }
        for (Course course : courses){
            coureNameList.add(course.getName());
        }
        campusPageResponse.setStudents(studentNameList);
        campusPageResponse.setTeachers(teacherNameList);
        campusPageResponse.setClasses(coureNameList);

        campusPageResponse.setDateFrom(DateUtil.isoStrToTargetStr(starttime, "yyyy/MM/dd"));
        campusPageResponse.setDateTo(DateUtil.isoStrToTargetStr(endtime, "yyyy/MM/dd"));
        campusPageResponse.setCampusPic("");//暂未知
        campusPageResponse.setName(classroom.getName());
    }

    /**
     * 获取考勤信息
     * @param campusPageResponse
     * @param date
     * @param reportsMap
     * @param studentMap
     */
    public static void setStatisticsInfo1(CampusPageResponse campusPageResponse, String date,
                                          Map<String,Reports> reportsMap,Map<String,Student> studentMap,
                                          Map<String,StudentModel> studentModelMap){
        //昂立接口该班级实际学生
        Set<String> angliSet = studentModelMap.keySet();

        StatisticsInfo1 statisticsInfo1 = new StatisticsInfo1();
        statisticsInfo1.setDate(date);
        List<StudentInfo> studentInfos = new ArrayList<>();
        List<RegInfo> regInfo = new ArrayList<>();
        StudentInfo studentInfo;
        RegInfo reg;
        Student student;
        Reports reports;
        Integer yingdao =  angliSet.size();
        Integer shidao = 0;
        Integer queqin = 0;
        Iterator<String> keys = reportsMap.keySet().iterator();
        while (keys.hasNext()){
            String key = keys.next();
            student = studentMap.get(key);
            reports = reportsMap.get(key);
            studentInfo = new StudentInfo(student.getName_ch(), student.getAvatar_url());
            studentInfos.add(studentInfo);
            int student_count = Integer.valueOf(reports.getStudent_count());
            if(shidao == 0){
                shidao = student_count;
            } else {
                if (shidao < student_count){
                    shidao = student_count;
                }
            }
            reg = new RegInfo(student.getName_ch(), DateUtil.isoStrToTargetStr(
                    reports.getStart_time(), "HH:mm"));
            regInfo.add(reg);
        }
        Info1Values values = new Info1Values();
        values.setChuqinlv(CommonUtils.getNum(((float)shidao/yingdao)*100.0f, "0.0") +"");
        values.setYingdao(yingdao);
        values.setShidao(shidao);
        values.setQueqin(yingdao - shidao);
        statisticsInfo1.setValues(values);
        statisticsInfo1.setRegInfo(regInfo);
        campusPageResponse.setStatisticsInfo1(statisticsInfo1);
        campusPageResponse.setStudentInfo(studentInfos);
    }

    public static List mapToList(Map map){
        List list = new ArrayList();
        Iterator<String> it = map.keySet().iterator();
        while(it.hasNext()){
            list.add(map.get(it.next()));
        }
        return  list;
    }


    public static void main(String[] args){
        String s = JSONObject.toJSONString(CampusDataUtil.reponseData("2019-03-13T00:00:00.000Z","2019-03-14T00:00:00.000Z",
                "5bda63286a525c640fd12229"));
        System.out.println(s);
    }

}
